(library (lib list-utils)
  (export member-recursively
          multirember-with-equal-proc
          remove-member-recursively)
  (import (rnrs base)
          (only (guile)
                lambda* λ)
          (srfi srfi-1)))


(define rest cdr)


(define member-recursively
  (lambda* (atom lst #:key (test eq?))
    (cond
     [(null? lst) #f]
     [(pair? (first lst))
      (or (member-recursively atom (first lst) #:test test)
          (member-recursively atom (rest lst) #:test test))]
     [else
      (cond
       [(test atom (first lst)) lst]
       [else (member-recursively atom (rest lst) #:test test)])])))


(define multirember-with-equal-proc
  (λ (equal-proc)
    (λ (lst unwanted)
      (let loop ([remaining-list lst])
        (cond
         [(null? remaining-list)
          '()]
         [(equal-proc (first remaining-list) unwanted)
          (loop (rest remaining-list))]
         [(pair? (first remaining-list))
          (cons (loop (first remaining-list))
                (loop (rest remaining-list)))]
         [else
          (cons (first remaining-list)
                (loop (rest remaining-list)))])))))


(define remove-member-recursively
  (lambda* (lst unwanted #:key (test symbol=?))
    ((multirember-with-equal-proc test) lst unwanted)))
